package day14;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.*;

//(面向对象+集合+IO流+线程+线程池+Socket)
// 模拟用户登录注册聊天功能(私聊)
public class AccountSystem {
    static Scanner sc = new Scanner(System.in);
    static List<Account> list = new ArrayList<>();

    public static void main(String[] args) {
        one();
        //System.out.println(md5(md5("123")));
        //System.out.println(md5("abc"));
    }

    public static void one() {
        while (true) {
            System.out.println("======用户私聊系统======");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            System.out.println("请输入编号");
            int n = sc.nextInt();
            switch (n) {
                case 1:
                    login();
                    break;
                case 2:
                    register();
                    break;
                case 3:
                    return;
            }
        }
    }

    public static void two(Account a) {
        System.out.println(a.username + "登录成功");
        while (true) {
            System.out.println("1.聊天功能");
            System.out.println("2.退出登录");   //清除缓存
            System.out.println("0.返回上级");
            int n = sc.nextInt();
            switch (n) {
                case 1:
                    break;
                case 2:
                    break;
                case 0:
                    return;
            }
        }

    }

    //如果第一次登录成功  可以记录缓存(登录成功的记录)
    //如果下一次再登录  还是相同的账号 就不需要输入密码 直接免登录
    //缓存信息 不能一直存在  30秒之后失效
    public static void login() {
        System.out.println("请输入账号");
        String account = sc.next();
        //验证是否登录过 --> two();
        Account acc = checkCookie(account);
        if (acc != null) {
            two(acc);
            return;
        }
        System.out.println("请输入密码");
        String password = sc.next();
        for (Account a : list) {
            if (account.equals(a.username) && password.equals(a.password)) {
                System.out.println("是否保存本地缓存y/n,30秒内免登录");
                String yn = sc.next();
                if ("y".equals(yn)) {
                    addCookie(a);
                    //创建可周期定长线程池    30S首次延迟功能
                    ScheduledExecutorService ses = new ScheduledThreadPoolExecutor(1);
                    ses.scheduleAtFixedRate(()->{

                    },30,30,TimeUnit.SECONDS);
                    ses.shutdown();
                }

                two(a);
                return;
            }
        }
    }

    private static Account checkCookie(String account) { //验证是否登录
        try (BufferedReader br =
                     new BufferedReader(
                             new InputStreamReader(
                                     new FileInputStream("d://a//cookie.txt")))) {
            String msg = br.readLine();
            //将账号加密后和之前保存的加密信息  比较是否一样
            if (msg != null && msg.equals(md5(account))) {
                for (Account a : list) {
                    if (a.username.equals(account)) return a;
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("首次登录");
        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String md5(String msg) {  //不可逆加密处理
        StringBuilder hexString=null;
        try {
            // 获取 MessageDigest 类的实例，指定算法为 MD5
            MessageDigest digest = MessageDigest.getInstance("MD5");
            // 对字符串进行加密
            byte[] encodedHash = digest.digest(msg.getBytes());
            // 将加密后的字节数组转换为十六进制字符串
            hexString = new StringBuilder();
            for (byte b : encodedHash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return hexString.toString();
    }

    public static void addCookie(Account a) { //本地缓存
        try (FileOutputStream fos = new FileOutputStream("d://a//cookie.txt")) {
            fos.write(md5((a.username)).getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public static void register() {
        System.out.println("请输入账号");
        String account = sc.next();
        System.out.println("请输入密码");
        String password = sc.next();
        Account a = new Account(account, password);
        //存储集合
        list.add(a);
        //做序列化

    }

}

class Account {
    String username;
    String password;

    public Account(String username, String password) {
        this.username = username;
        this.password = password;
    }
}